package com.xiaoshou.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import com.xiaoshou.pojo.Question;
import com.xiaoshou.pojo.Questiontype;
import com.xiaoshou.pojo.User;

@Controller
@Results({
		@Result(name = "tomain", location = "main.jsp", type = "dispatcher", params = {
				"fileTypesList", "${fileTypesList}", "questionsList",
				"${questionsList}", "user", "${user}", "nowPage", "${nowPage}",
				"totalcount", "${totalcount}", "maxPage", "${maxPage}", "time",
				"${time}", "pagenum", "${pagenum}", "", "", }),
		@Result(name = "toRelogin", location = "login.action", type = "redirectAction", params = {
				"task", "${task}" }) })
public class IndexAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	public final static String indexDirForQuestion = "F:\\工作相关索引\\销售索引";
	private List<Question> questionsList;
	private String task;
	private List<String> pagenum;
	private int startindex;
	private int endindex;
	private int nowPage;
	private int maxPage;
	private int totalcount;
	private List<Document> documentList;
	private List<Questiontype> questionTypesList;
	private String searchQuestion;
	private double time;

	private List<Question> salesQuestionTrendsList;

	@Override
	public String execute() throws Exception {
		return null;
	}

	@Action("relogin")
	public String reLogin() {

		return "toRelogin";
	}

	@Action("index")
	public String toIndexPage() {
		long starttime = new Date().getTime();

		pagenum = new ArrayList<String>();
		questionTypesList = getQuestionTypeService().findAllQuestionList();

		findAllByPaging();
		time = (double) (new Date().getTime() - starttime) / 1000;
		salesQuestionTrendsList = getQuestionService().findQuestionsTrendsBySales((User)getFromSession("user"));
		return "tomain";
	}

	/**
	 * 全查分页
	 */
	public void findAllByPaging() {
		totalcount = getQuestionService().getQuestionDao().findAll().size();
		if (nowPage < 0) {
			nowPage = 0;
		} else if (nowPage == 0) {
			questionsList = getQuestionService().findQuestionsListByNowpage(
					nowPage);
		}
		if (totalcount <= 10) {
			pagenum.add("1");
			maxPage = 1;
		} else if (totalcount % 10 != 0) {//
			maxPage = totalcount / 10 + 1; //
			if (maxPage < 7) {
				for (int i = 1; i < maxPage + 1; i++) {
					pagenum.add(String.valueOf(i));
				}
			} else {
				if (nowPage < 4) {
					for (int i = 1; i < 7; i++) {
						pagenum.add(String.valueOf(i));
					}
				} else if (nowPage >= (maxPage - 3)) {
					for (int i = (maxPage - 6); i < maxPage; i++) {
						pagenum.add(String.valueOf(i + 1));
					}
				} else {
					pagenum.add(String.valueOf(nowPage - 1));
					pagenum.add(String.valueOf(nowPage));
					pagenum.add(String.valueOf(nowPage + 1));
					pagenum.add(String.valueOf(nowPage + 2));
					pagenum.add(String.valueOf(nowPage + 3));
					pagenum.add(String.valueOf(nowPage + 4));
				}
			}
		} else if (totalcount % 10 == 0) {//
			maxPage = totalcount / 10; // 页数
			for (int i = 1; i < maxPage + 1; i++) {
				pagenum.add(String.valueOf(i));
			}
		}
		if (nowPage != 0 && (nowPage + 1) < (maxPage)) {
			startindex = nowPage * 10;
			endindex = startindex + 9;
			questionsList = getQuestionService().findQuestionsListByNowpage(
					nowPage);
		} else if (nowPage != 0 && (nowPage + 1) == maxPage) {
			startindex = nowPage * 10;
			endindex = totalcount;
			questionsList = getQuestionService().findQuestionsListByNowpage(
					nowPage);
		}
	}

	// set get
	public List<Question> getQuestionsList() {
		return questionsList;
	}

	public void setQuestionsList(List<Question> questionsList) {
		this.questionsList = questionsList;
	}

	public String getTask() {
		return task;
	}

	public void setTask(String task) {
		this.task = task;
	}

	public List<String> getPagenum() {
		return pagenum;
	}

	public void setPagenum(List<String> pagenum) {
		this.pagenum = pagenum;
	}

	public int getStartindex() {
		return startindex;
	}

	public void setStartindex(int startindex) {
		this.startindex = startindex;
	}

	public int getEndindex() {
		return endindex;
	}

	public void setEndindex(int endindex) {
		this.endindex = endindex;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public int getTotalcount() {
		return totalcount;
	}

	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}

	public List<Document> getDocumentList() {
		return documentList;
	}

	public void setDocumentList(List<Document> documentList) {
		this.documentList = documentList;
	}

	public String getSearchQuestion() {
		return searchQuestion;
	}

	public void setSearchQuestion(String searchQuestion) {
		this.searchQuestion = searchQuestion;
	}

	public double getTime() {
		return time;
	}

	public void setTime(double time) {
		this.time = time;
	}

	public List<Questiontype> getQuestionTypesList() {
		return questionTypesList;
	}

	public void setQuestionTypesList(List<Questiontype> questionTypesList) {
		this.questionTypesList = questionTypesList;
	}

	public List<Question> getSalesQuestionTrendsList() {
		return salesQuestionTrendsList;
	}

	public void setSalesQuestionTrendsList(
			List<Question> salesQuestionTrendsList) {
		this.salesQuestionTrendsList = salesQuestionTrendsList;
	}

}
